﻿using Prism.DryIoc;
using Prism.Ioc;
using System;
using System.Threading;
using System.Windows;
using TiaoTiaoCode.AutoUpdate.ClientUI.MainFrame;
using TiaoTiaoCode.AutoUpdate.Common.Exceptions;

namespace TiaoTiaoCode.AutoUpdate.ClientUI
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainView>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {

        }

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        static Mutex? _mutex;

        protected override void OnStartup(StartupEventArgs e)
        {
            bool createNew;
            _mutex = new Mutex(true, "TiaoTiaoUpgrade", out createNew);
            if (createNew)
                base.OnStartup(e);
            else
                Shutdown();
        }

        private void PrismApplication_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;

            if (e.Exception is DivideByZeroException dex)
                MessageBox.Show($"发生异常，但可继续运行\r\n{dex.Message}");
            else
                MessageBox.Show($"程序异常：{e.Exception.Message}");
        }
    }
}
